package cn.ailanyin.home.module.category.service.impl;

import cn.ailanyin.home.common.context.UserInfoContext;
import cn.ailanyin.home.common.exception.ServiceException;
import cn.ailanyin.home.common.utils.VOUtil;
import cn.ailanyin.home.module.category.dto.CategoryDTO;
import cn.ailanyin.home.module.category.entity.Category;
import cn.ailanyin.home.module.category.mapper.CategoryMapper;
import cn.ailanyin.home.module.category.service.ICategoryService;
import cn.ailanyin.home.module.category.vo.CategoryVO;
import cn.ailanyin.home.module.things.entity.Things;
import cn.ailanyin.home.module.things.mapper.ThingsMapper;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 小怪兽
 * @version 1.0
 * @date 2024/4/13 0013 12:29:01
 */
@Service
@RequiredArgsConstructor
public class CategoryServiceImpl implements ICategoryService {
    /**
     * 分类映射器
     */
    private final CategoryMapper categoryMapper;

    /**
     * 事物Mapper
     */
    private final ThingsMapper thingsMapper;


    @Override
    public List<CategoryVO> list(CategoryDTO dto) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(dto.getName()), Category::getName, dto.getName())
            .eq(Category::getHomeId, UserInfoContext.getHomeId());
        List<Category> list = categoryMapper.selectList(wrapper);
        return VOUtil.getCategoryVOs(list);
    }

    @Override
    public void saveOrUpdate(CategoryDTO dto) {
        String name = dto.getName();
        if (StrUtil.isBlank(name)) {
            throw new ServiceException("分类名不能为空！");
        }
        isExists(dto);
        if (StrUtil.isBlank(dto.getId())) {
            // 新增
            Category category = new Category();
            category.setName(name);
            categoryMapper.insert(category);
            return;
        }
        // 修改
        LambdaUpdateWrapper<Category> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(StrUtil.isNotBlank(name), Category::getName, name)
            .eq(Category::getId, dto.getId())
            .eq(Category::getHomeId, UserInfoContext.getHomeId());
        categoryMapper.update(wrapper);
    }

    @Override
    public void delete(String id) {
        // 判断分类下是否有物品, 有物品, 提示无法删除
        LambdaQueryWrapper<Things> thingsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        thingsLambdaQueryWrapper.eq(Things::getCategoryId, id);
        Long count = thingsMapper.selectCount(thingsLambdaQueryWrapper);
        if (count > 0) {
            throw new ServiceException("分类绑定了物品，无法删除");
        }
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getHomeId, UserInfoContext.getHomeId())
            .eq(Category::getId, id);
        categoryMapper.delete(wrapper);
    }

    private void isExists(CategoryDTO dto) {
        String name = dto.getName();
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(name), Category::getName, name)
            .eq(Category::getHomeId, UserInfoContext.getHomeId())
            .ne(StrUtil.isNotBlank(dto.getId()), Category::getId, dto.getId());
        boolean exists = categoryMapper.exists(wrapper);
        if (exists) {
            throw new ServiceException("分类名已存在！");
        }
    }


}
